package com.example.homeworkmanagement.controller;


import com.example.homeworkmanagement.pojo.Classes;
import com.example.homeworkmanagement.service.ClassesService;
import com.example.homeworkmanagement.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Array;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 拒绝加班
 * @since 2025-05-25
 */
@RestController
@RequestMapping("/classes")
@Api(tags = "班级信息管理", description = "提供管理API接口")
@CrossOrigin
public class ClassesController {
    @Autowired
    private ClassesService classesService;


    /**
     * 获取所有班级列表
     */
    @PostMapping("/getClassList")
    @ApiOperation(value = "获取所有班级列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "classId", value = "班级id", required = false),
            @ApiImplicitParam(dataType = "string", name = "grade", value = "年级", required = false),
            @ApiImplicitParam(dataType = "string", name = "major", value = "专业", required = false),
            @ApiImplicitParam(dataType = "string", name = "department", value = "院系", required = false),
            @ApiImplicitParam(dataType = "string", name = "className", value = "班级名字", required = false),
            @ApiImplicitParam(dataType = "integer", name = "page", value = "页码", required = true, defaultValue = "1"),
            @ApiImplicitParam(dataType = "integer", name = "size", value = "每页大小", required = true,  defaultValue = "10")
    })
    public ResultVO<List<Classes>> getClassList(Classes classes, Integer page, Integer size) {
        System.out.println("接收参数 - classes: " + classes);
        System.out.println("接收参数 - page: " + page + ", size: " + size);
        // 处理 page 为 null 的情况，设置默认值
        if (page == null || page <= 0) {
            page = 1;
        }
        // 处理 size 为 null 的情况，设置默认值
        if (size == null || size <= 0) {
            size = 10;
        }
        return classesService.getClassList(classes, page, size);
    }

    /**
     * 根据班级ID获取班级详情
     */
    @GetMapping("/getClassDetail")
    @ApiOperation(value = "根据班级ID获取班级详情")
    @ApiImplicitParam(name = "classId", value = "班级id", required = true, dataType = "string")
    public ResultVO<Classes> getClassDetail(String classId) {
        return classesService.getClassDetail(classId);
    }

    /**
     * 添加班级
     */
    @PostMapping("/addClass")
    @ApiOperation(value = "添加班级")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "classId", value = "班级id（必须唯一）", required = true),
            @ApiImplicitParam(dataType = "string", name = "grade", value = "年级", required = true),
            @ApiImplicitParam(dataType = "string", name = "major", value = "专业", required = true),
            @ApiImplicitParam(dataType = "string", name = "department", value = "院系", required = true),
            @ApiImplicitParam(dataType = "string", name = "className", value = "班级名字", required = true)
    })
    public ResultVO<Classes> addClass(Classes classes) {
        return classesService.addClass(classes);
    }

    /**
     * 更新班级信息
     */
    @PostMapping("/updateClass")
    @ApiOperation(value = "更新班级信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "classId", value = "班级id（必须存在）", required = true),
            @ApiImplicitParam(dataType = "string", name = "grade", value = "年级", required = false),
            @ApiImplicitParam(dataType = "string", name = "major", value = "专业", required = false),
            @ApiImplicitParam(dataType = "string", name = "department", value = "院系", required = false),
            @ApiImplicitParam(dataType = "string", name = "className", value = "班级名字", required = false)
    })
    public ResultVO<Classes> updateClass(Classes classes) {
        return classesService.updateClass(classes);
    }

    /**
     * 删除班级（根据班级ID）
     */
    @DeleteMapping("/deleteClass")
    @ApiOperation(value = "删除班级")
    @ApiImplicitParam(name = "classId", value = "班级id", required = true, dataType = "string")
    public ResultVO<Classes> deleteClass(String classId) {
        return classesService.deleteClass(classId);
    }

    /**
     * 根据班级名称列表获取班级ID列表
     */
    @PostMapping("/getClassIdsByNames")
    @ApiOperation(value = "根据班级名称列表获取班级ID列表")
    @ApiImplicitParam(name = "classNames", value = "班级名称列表", required = true, dataType = "list")
    public ResultVO<String> getClassIdsByNames(@RequestBody List<String> classNames) {
//        classNames.forEach(System.out::println);
        return classesService.getClassIdsByNames(classNames);
    }

    /**
     * 根据班级ID串获取班级名称串
     */
    @PostMapping("/getClassNamesByIds")
    @ApiOperation(value = "根据班级ID串获取班级名称串")
    @ApiImplicitParam(name = "classIds", value = "班级ID列表串", required = true, dataType = "string")
    public ResultVO<String> getClassNamesByIds(String classIds) {
//        classNames.forEach(System.out::println);
        return classesService.getClassNamesByIds(classIds);
    }

}
